Precondition: You have installed and created a project template
Templates default to PostgreSQL
as a relational database and Redis
as distributed cache support.
You need to configure the connection string in appsettings.json
and replace the default database if you want.
Entity
projectDbSet
underEntityFramework
projectImportant
Please follow the official documentation ofEntity Framework Core
to define models and relationships.
Usedry studio
to generatedto
,Manager
,Controller
and other basic code.
Tip
For more information aboutdry cli
, seethe dry cli documentation.
Implement business logic in theApplication``Manager
directory, usually including Filter Query
, Add Entity
, and Update Entity
.
Steps to build custom query criteria:
Queryable
FilterAsync<T>
method to get resultsCode example:
public override async Task<PageList<DiscussItemDto>> FilterAsync(DiscussFilterDto filter) { // 根据实际业务构建筛选条件 if (filter.IsTop != null) { Queryable = Queryable.Where(q => q.IsTop == filter.IsTop); } if (filter.MemberId != null) { Queryable = Queryable.Where(q => q.Member.Id == filter.MemberId); } return await Query.FilterAsync<DiscussItemDto>(Queryable, filter.PageIndex, filter.PageSize); }
Code example:
public override async Task<Discuss> AddAsync(Discuss entity) { var res = await Command.CreateAsync(entity); // TODO: do something // 提交更新 await Command.SaveChangeAsync(); return res; }
Manager
provides a GetCurrentAsync
method to get the current (writable dbcontext
) entity.
In the controller, the entity will be obtained first. If it does not exist, it will directly return to404
.
The entity update method passes two parameters, one for the entity itself and one for the submittedDTO
object.
The entity itself is obtained by usingGetCurrentAsync
method in the controller, and can be passed directly as a parameter.
Code example:
The following aspects demonstrate how to update associated content.
public override async Task<ResourceTypeDefinition> UpdateAsync(ResourceTypeDefinition entity, ResourceTypeDefinitionUpdateDto dto) { // 更新关联的内容 entity!.AttributeDefines = null; if (dto.AttributeDefineIds != null) { // 通过父类的 Stores 查询其他实体的内容 var attributeDefines = await Stores.ResourceAttributeDefineCommand.Db.Where(a => dto.AttributeDefineIds.Contains(a.Id)).ToListAsync(); entity.AttributeDefines = attributeDefines; } return await base.UpdateAsync(entity, dto); }
Manager
provides default query method for details, which can directly pass query conditions.
If you customize queries, such as querying associated content, you need to add new methods to implement them.
Manager
provides Query.Db
members to query directly against the current model.
Code example:
public async Task<ResourceGroup?> FindAsync(Guid id) { return await Query.Db .Include(g => g.Environment) .FirstOrDefaultAsync(g => g.Id == id); }
Delete defaults to soft delete, and if you want to modify this behavior, setEnableSoftDelete
to false in your Manager
class. like:
Command.EnableSoftDelete = false;
Deletion sometimes involvesassociation deletion, example code:
public override async Task<Resource?> DeleteAsync(Guid id) { var resource = entity; if (resource!.Attributes != null) { Stores.CommandContext.RemoveRange(resource.Attributes); } return await DeleteAsync(resource); }
After you implement the business interface and implementation inApplication
, you can call the related business methods in the interface class inHttp.API
through dependency injection.
Note
Please keep the code of the controller clean and avoid查询数据库、实现业务逻辑
directly in the controller.
After you add or modify entities, you need to perform a database migration for the database structure changes to take effect.
You can usedotnet ef
command to generate migration scripts.
Important
Database migration is required underHttp.API
project.
Or you can simply execute the migration generationscript/EFMigrations.ps1
we provide.
Tip
You don't need to executedotnet ef update
because the program commits changes automatically when it starts.